<!--
 * @Description: 财务管理-开具发票
 * @Author: Li Yujie
 * @Date: 2021/01/21
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2021/01/21
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    开具发票
                </div>
            </div>
            <!-- /header -->
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        店铺名称
                    </p>
                    <el-input
                        placeholder="请输入店铺名称"
                        v-model="filterData.shop_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        发票抬头
                    </p>
                    <el-input
                        placeholder="请输入发票抬头"
                        v-model="filterData.title"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        开票状态
                    </p>
                    <el-select
                        v-model="filterData.status"
                        filterable
                        placeholder="全部"
                        size="medium"
                        style="width: 152px"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        申请时间
                    </p>
                    <el-date-picker
                        v-model="dateTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="medium"
                        style="width: 400px"
                        value-format="timestamp"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->

            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
                ref="tempLateName"
                class="data-table"
                v-loading="tableLoading"
            >
                <el-table-column
                    label="序号"
                    align="center"
                    width="80"
                    type="index"
                />
                <el-table-column
                    prop="shop_title"
                    label="店铺名称"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="发票类型"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.type === 1">电子发票</div>
                        <div v-if="scope.row.type === 2">纸质发票</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="抬头类型"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.title_type === 1">企业单位</div>
                        <div v-if="scope.row.title_type === 2">个人/非企业单位</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="title"
                    label="发票抬头"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="发票信息"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="medium"
                            type="text"
                            @click="showDetailInfo(scope.row, 'detail')"
                        >查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="财务核对"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="medium"
                            type="text"
                            @click="showCheckDialog(scope.row)"
                        >查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="发票金额"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div>{{ scope.row.money }}元</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="开票状态"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.status === 0">未开票</div>
                        <div v-if="scope.row.status === 1">已开票</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="申请时间"
                    align="center"
                    width="200"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="开票时间"
                    align="center"
                    width="200"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.invoice_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="left"
                    width="240"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="medium"
                            v-if="scope.row.status === 0"
                            @click="handleMakeInvoice(scope.row)"
                        >
                            开票
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            v-if="scope.row.status === 0"
                            @click="showDetailInfo(scope.row, 'edit')"
                        >
                            修改发票信息
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="showLogistics(scope.row)"
                            v-if="scope.row.status !== 0 && !scope.row.receive_tracking_no && scope.row.type === 2"
                        >
                            填写物流单号
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="showLogistics(scope.row, 'update')"
                            v-if="scope.row.status !== 0 && scope.row.receive_tracking_no && scope.row.type === 2"
                        >
                            修改物流单号
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>

        <!--查看发票信息-->
        <el-dialog
            title="发票信息"
            :visible.sync="infoDialog"
            width="680px"
            :close-on-click-modal="false"
        >
            <div class="info-dialog-wrapper">
                <el-form
                    :model="detailInfo"
                    :rules="rules"
                    label-width="84px"
                    label-position="right"
                    :class="detailType === 'detail' ? 'rule-form' : ''"
                >
                    <template v-if="detailInfo.title_type === 2">
                        <el-form-item
                            label="姓名："
                        >
                            <div v-if="detailType === 'detail'">{{ detailInfo.title }}</div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.title"
                                    size="medium"
                                    style="width: 400px"
                                />
                            </div>
                        </el-form-item>
                    </template>
                    <template v-if="detailInfo.title_type === 1">
                        <el-form-item
                            label="公司名称："
                        >
                            <div v-if="detailType === 'detail'">{{ detailInfo.title }}</div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.title"
                                    size="medium"
                                    style="width: 400px"
                                />
                            </div>
                        </el-form-item>
                        <el-form-item
                            label="公司税号："
                        >
                            <div v-if="detailType === 'detail'">{{ detailInfo.tax_reg_cert_number }}</div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.tax_reg_cert_number"
                                    size="medium"
                                    style="width: 400px"
                                />
                            </div>
                        </el-form-item>
                        <template v-if="detailInfo.type === 2">
                            <el-form-item
                                label="公司地址："
                            >
                                <div v-if="detailType === 'detail'">{{ detailInfo.company_address }}</div>
                                <div v-if="detailType === 'edit'">
                                    <el-input
                                        v-model="detailInfo.company_address"
                                        size="medium"
                                        style="width: 400px"
                                    />
                                </div>
                            </el-form-item>
                            <el-form-item
                                label="公司电话："
                            >
                                <div v-if="detailType === 'detail'">{{ detailInfo.company_tel }}</div>
                                <div v-if="detailType === 'edit'">
                                    <el-input
                                        v-model="detailInfo.company_tel"
                                        size="medium"
                                        style="width: 400px"
                                    />
                                </div>
                            </el-form-item>
                            <el-form-item
                                label="开户银行："
                            >
                                <div v-if="detailType === 'detail'">{{ detailInfo.bank_name }}</div>
                                <div v-if="detailType === 'edit'">
                                    <el-input
                                        v-model="detailInfo.bank_name"
                                        size="medium"
                                        style="width: 400px"
                                    />
                                </div>
                            </el-form-item>
                            <el-form-item
                                label="开户账号："
                            >
                                <div v-if="detailType === 'detail'">{{ detailInfo.bank_account }}</div>
                                <div v-if="detailType === 'edit'">
                                    <el-input
                                        v-model="detailInfo.bank_account"
                                        size="medium"
                                        style="width: 400px"
                                    />
                                </div>
                            </el-form-item>
                        </template>
                        <el-form-item
                            label="备注："
                        >
                            <div v-if="detailType === 'detail'">{{ detailInfo.remark ? detailInfo.remark : '--' }}</div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.remark"
                                    size="medium"
                                    style="width: 400px"
                                />
                            </div>
                        </el-form-item>
                    </template>
                    <div style="padding-top: 20px;margin-top: 20px; border-top: 1px solid #ccc">
                        <el-form-item
                            label="电子邮箱："
                            v-if="detailInfo.type === 1"
                        >
                            <div v-if="detailType === 'detail'">{{ detailInfo.receive_email ? detailInfo.receive_email : '--' }}</div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.receive_email"
                                    size="medium"
                                    style="width: 400px"
                                />
                            </div>
                        </el-form-item>
                        <el-form-item
                            label="手机号码："
                            v-if="detailInfo.type === 2"
                        >
                            <div v-if="detailType === 'detail'">{{ detailInfo.receive_address ? detailInfo.receive_address.phone : '--' }}</div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.receive_address.phone"
                                    size="medium"
                                    style="width: 400px"
                                />
                            </div>
                        </el-form-item>
                        <el-form-item
                            label="手机号码："
                            v-if="detailInfo.type === 1"
                        >
                            <div v-if="detailType === 'detail'">{{ detailInfo.receive_phone ? detailInfo.receive_phone : '--' }}</div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.receive_phone"
                                    size="medium"
                                    style="width: 400px"
                                />
                            </div>
                        </el-form-item>
                        <el-form-item
                            label="收货地址："
                            v-if="detailInfo.type === 2"
                        >
                            <div v-if="detailInfo.receive_address && detailType === 'detail'">
                                {{ detailInfo.receive_address.province_name }}
                                {{ detailInfo.receive_address.city_name }}
                                {{ detailInfo.receive_address.county_name }}
                                {{ detailInfo.receive_address.detail_info }}
                            </div>
                            <div v-if="detailType === 'edit'">
                                <el-cascader
                                    ref="citySelect"
                                    placeholder="活动地区"
                                    :options="cities"
                                    filterable
                                    size="medium"
                                    clearable
                                    expand-trigger="hover"
                                    v-model="citySelect"
                                    :props="{
                                        value: 'name',
                                        label: 'name',
                                        children: 'children'
                                    }"
                                />
                            </div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.receive_address.detail_info"
                                    placeholder="详细地址"
                                    size="medium"
                                    style="width: 400px;margin-top: 22px"
                                />
                            </div>
                        </el-form-item>
                        <el-form-item
                            label="收件人："
                            v-if="detailInfo.type === 2"
                        >
                            <div v-if="detailType === 'detail'">{{ detailInfo.receive_address ? detailInfo.receive_address.user_name : '--' }}</div>
                            <div v-if="detailType === 'edit'">
                                <el-input
                                    v-model="detailInfo.receive_address.user_name"
                                    size="medium"
                                    style="width: 400px"
                                />
                            </div>
                        </el-form-item>
                    </div>
                </el-form>
                <div class="btn-group">
                    <el-button
                        size="middle"
                        class="close-btn"
                        @click="infoDialog = false"
                    >
                        关闭
                    </el-button>
                    <el-button
                        type="primary"
                        size="middle"
                        class="close-btn"
                        v-if="detailType === 'detail' && detailInfo.status === 0"
                        @click="detailType = 'edit'"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="primary"
                        class="close-btn"
                        size="middle"
                        v-if="detailType === 'edit' && detailInfo.status === 0"
                        @click="submitInvoice"
                    >
                        保存
                    </el-button>
                </div>
            </div>
        </el-dialog>
        <!--核对财务信息弹窗-->
        <finance-check
            ref="financeCheck"
        />

        <!--填写物流弹窗-->
        <change-logistics-dialog
            ref="changeLogisticsDialog"
            @change="handleChangeLogistics"
        />
    </div>
</template>

<script>
import ChangeLogisticsDialog from '@/components/dialog/change-logistics-dialog';
import FinanceCheck from '@/components/finance/finance-check';
import { back } from '@/mixin/back';
import Template from "../../template/template";

export default {
    name: 'Invoice',
    mixins: [back],
    components: {
        Template,
        ChangeLogisticsDialog,
        FinanceCheck
    },
    data() {
        return {
            filterData: {
                // 审核状态
                status: 99,
                // 发票抬头
                title: '',
                // 店铺名称
                shop_keyword: '',
                // 申请时间
                create_start_time: '',
                create_end_time: ''
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: [],
            // 审核类型
            statusList: [
                {label: '全部', value: 99},
                {label: '未开票', value: 0},
                {label: '已开票', value: 1},
            ],
            // 查看信息弹窗
            infoDialog: false,
            // 详细信息
            detailInfo: {},
            rules: {},
            // 详细信息查看detail 编辑edit
            detailType: 'detail',
            invoice_no: '',
            authResult: {},
            cities: [],
            citySelect: []
        };
    },
    computed: {},
    watch: {
        dateTimeRange(value) {
            this.filterData.create_start_time = value ? value[0] / 1000 : '';
            this.filterData.create_end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },

    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '财务管理'}, {title: '开具发票'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
        this.getCities();
    },

    methods: {
        /**
         * @description 获取省市区
         */
        getCities() {
            this.$post('/student/local_info%5Cget_deliver_area_list', {}, res => {
                if (res.code === 1) {
                    this.cities = res.data.list;
                }
            });
        },

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {

            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;

            this.$post('/student/hire_invoice%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                }
                this.tableLoading = false;
            });
        },
        /**
         * @description 查看 详细发票信息
         */
        showDetailInfo(row, type) {
            if(JSON.stringify(row.receive_address) === '[]') {
                row.receive_address = {};
            }
            this.detailInfo = {...row};
            this.citySelect = [this.detailInfo.receive_address.province_name, this.detailInfo.receive_address.city_name,this.detailInfo.receive_address.county_name];
            this.detailType = type;
            this.infoDialog = true;
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 显示物流设置弹窗
         * @param row
         * @param type 修改update
         */
        showLogistics(row, type) {
            this.invoice_no = row.invoice_no;
            if(type === 'update') {
                this.$refs.changeLogisticsDialog.show(row.receive_logistics_no, row.receive_tracking_no);
            } else {
                this.$refs.changeLogisticsDialog.show();
            }
        },
        /**
         * 填写物流
         * @param val
         */
        handleChangeLogistics(val) {
            let params = {
                invoice_no: this.invoice_no,
                logistics_no: val.logistics_no,
                tracking_no: val.tracking_no
            };

            this.$post('/student/hire_invoice%5Cset_receive_tracking_info', params, res => {
                if (res.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '设置成功',
                        type: 'success',
                        duration: 2000
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });

        },

        /**
         * 确认开票
         * @param row
         */
        handleMakeInvoice(row) {
            if(!this.authResult.auth_issue) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$confirm('请确认是否已经为企业开具发票？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    invoice_no : row.invoice_no
                };

                this.$post('/student/hire_invoice%5Cissue_invoice',params, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            title: '成功',
                            message: '开票成功',
                            type: 'success',
                            duration: 2000
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * 显示财务核对弹窗
         * @param row
         */
        showCheckDialog(row) {
            this.$refs.financeCheck.show(row.hire_no);
        },

        submitInvoice() {
            let params = {...this.detailInfo};

            params.receive_address_phone = params.receive_address.phone;
            params.receive_address_user_name = params.receive_address.user_name;
            params.receive_address_detail_info = params.receive_address.detail_info;
            params.receive_address_province_name = this.citySelect[0];
            params.receive_address_city_name = this.citySelect[1];
            params.receive_address_county_name = this.citySelect[2];

            this.$post('/student/hire_invoice%5Cset',params, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '修改成功',
                        type: 'success',
                        duration: 2000
                    });
                    this.infoDialog = false;
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        }
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        padding: 24px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin-left: 12px;
                margin-bottom: 24px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            margin-top: 24px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 24px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }

            .input-with-select {
                width: 300px;
                margin-right: 32px;
            }

            /deep/ .input-with-select .el-input-group__prepend {
                background-color: #fff;
                width: 130px;
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    .info-dialog-wrapper {
        padding: 3px 22px;
        max-height: 600px;
        overflow-y: auto;
        &::-webkit-scrollbar {
            width: 4px;
        }
        .rule-form {
            /deep/ .el-form-item {
                margin-bottom: 0;
            }
        }

        /deep/ .el-form-item__content {
            line-height: 30px;
            margin-top: 6px;
        }
        .btn-group {
            display: flex;
            align-items: center;
            justify-content: center;
            .close-btn {
                width: 120px;
                height: 36px;
                display: flex;
                align-items: center;
                justify-content: center;
                margin-top: 24px;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    .el-textarea__inner {
        resize: none;
    }
}
</style>
